<template>
  <avue-crud class='tabs-table' :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form" @row-update="addUpdate" @row-del="rowDel" @row-save="rowSave" :before-open="beforeOpen" @selection-change="selectionChange" @refresh-change="refreshChange" @on-load="onLoad">
    <template slot="menuLeft">
      <el-button type="text" icon="el-icon-plus" class="rightbtn" @click="addRow">新增</el-button>
    </template>
  </avue-crud>
</template>

<script>
import { getProProcessList } from '@/api/project/productData/processData';
import { numparZReg, numParseReg, codeReg } from '@/util/zhengze';
import { init } from '@/mixins/init';

export default {
  mixins: [init],
  props: {
    productId: {
      type: String,
    },
    type: {
      type: String,
    },
    data: {
      type: Array,
    },
  },
  data() {
    return {
      option: {
        index: true,
        indexLabel: '序号',
        menuWidth: 150,
        addBtn: false,
        editBtn: false,
        addRowBtn: false,
        cellBtn: true,
        column: [
          {
            label: '工序名称',
            prop: 'procedureId',
            cell: true,
            type: 'select',
            dicData: [],
            props: {
              label: 'procedureName',
              value: 'id',
            },
            span: 12,
            minWidth: 140,
            rules: [
              {
                required: true,
                message: '请选择工序名称',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '人数(单班)',
            prop: 'procedureNum',
            cell: true,
            type: 'number',
            minRows: 1,
            maxRows: 99,
            precision: 0,
            rules: [
              {
                required: true,
                type: 'number',
                message: '请输入人数(单班)',
                trigger: 'blur',
              },
            ],
          },
          {
            label: '计划班次',
            prop: 'planClasses',
            cell: true,
            type: 'number',
            minRows: 1,
            maxRows: 99,
            precision: 0,
            rules: [{ type: 'number', required: true, message: '请输入计划班次', trigger: 'blur' }],
          },
          {
            label: '阶段人员总数',
            align: 'center',
            children: [
              {
                label: 'OTS人数',
                prop: 'otsNum',
                cell: true,
                type: 'number',
                minRows: 0,
                maxRows: 99,
                precision: 0,
              },
              {
                label: '小批',
                prop: 'tpNum',
                cell: true,
                type: 'number',
                minRows: 0,
                maxRows: 99,
                precision: 0,
              },
              {
                label: '量产',
                prop: 'amountNum',
                cell: true,
                type: 'number',
                minRows: 0,
                maxRows: 99,
                precision: 0,
              },
            ],
          },
          {
            label: '需新增人员数',
            prop: 'addNum',
            cell: true,
            span: 12,
            type: 'number',
            minRows: 0,
            maxRows: 99999,
            precision: 0,
          },
          {
            label: '人员基本能力要求',
            prop: 'requirements',
            type: 'textarea',
            minRows: 1,
            maxRows: 2,
            cell: true,
            maxlength: 60,
            minWidth: 120,
            overHidden: true,
          },
        ],
      },
    };
  },
  watch: {},
  computed: {},
  created() {
    if (this.type == 'edit') {
      this.option.header = true;
      this.option.menu = true;
    } else {
      this.option.header = false;
      this.option.menu = false;
    }
    getProProcessList({ productId: this.productId }).then((res) => {
      const data = res.data.data;
      this.findObject(this.option.column, 'procedureId').dicData = data;
    });
  },
  mounted() {},
  methods: {
    addRow() {
      this.$refs.crud.rowCellAdd({
        procedureNum: 1,
        planClasses: 1,
      });
    },
    rowSave(row, done, loading) {
      if (!numParseReg.test(row.procedureNum)) {
        this.$message.error('人数（单班）请输入大于0的整数');
        loading();
        return;
      }
      if (!numParseReg.test(row.planClasses)) {
        this.$message.error('计划班次请输入大于0的整数');
        loading();
        return;
      }
      this.data.push(row);
      done();
    },
    addUpdate(row, index, done, loading) {
      if (!numParseReg.test(row.procedureNum)) {
        this.$message.error('人数（单班）请输入大于0的整数');
        loading();
        return;
      }
      if (!numParseReg.test(row.planClasses)) {
        this.$message.error('计划班次请输入大于0的整数');
        loading();
        return;
      }
      this.data.splice(index, 1, row);
      done();
    },
    rowDel(row, index) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.data.splice(index, 1);
      });
    },
  },
};
</script>

<style lang="scss">
</style>
